package cn.harmonyoslearn.navrouter.models;

import cn.harmonyoslearn.navrouter.datas.InitNavInfo;
import cn.harmonyoslearn.navrouter.datas.JsonValue;
import cn.harmonyoslearn.navrouter.datas.enums.InitNavrouterModelEnum;
import cn.harmonyoslearn.navrouter.dialog.InitNavInfoDialog;
import cn.harmonyoslearn.navrouter.utils.CmdUtil;
import cn.harmonyoslearn.navrouter.utils.FileUtil;
import cn.harmonyoslearn.navrouter.utils.LogUtil;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.HashMap;
import java.util.Objects;

public class InitNavInfoModel {
    String basePath;
    public void actionPerformed(@NotNull AnActionEvent e) {
        basePath = FileUtil.getBasePath(e);
        Project project = e.getProject();

        // 弹窗获取信息
        InitNavInfoDialog initNavInfoDialog = new InitNavInfoDialog(project);
        initNavInfoDialog.show();

        if (!initNavInfoDialog.isOK()) {
            return;
        }

        InitNavInfo info = initNavInfoDialog.getInfo();
        if (info.model == InitNavrouterModelEnum.DEFAULT) {
            defaultInit();
        } else {
            this.customInit(info.navPageNames, info.model);
        }

        // 初始化ServiceModel
        ServiceModel.getInstance().init(Objects.requireNonNull(e.getProject()));

        // 执行ohpm脚本
        CmdUtil.run("ohpm install", e);
        // 刷新编辑窗口
        FileUtil.refreshAndFindFileByPath(basePath);
    }


    private void defaultInit() {
        try {
            // 将配置文件移动到根目录
            FileUtil.copyResourceFolderToProject("packages/routerInfo", this.basePath);
            // 将Navrouter模块添加到项目
            FileUtil.copyResourceFolderToProject("packages/common", this.basePath);
            FileUtil.refreshAndFindFileByPath(this.basePath);
            LogUtil.i("NewNavRouterModel copyResourceFolderToProject", "NavRouter模块复制成功");

            // 添加模块依赖引用
            JsonValue model = new JsonValue(new HashMap<>());
            model.addString("srcPath", "./common/NavRouter");
            model.addString("name", "NavRouter");

            String buildProfilePath = this.basePath + "/build-profile.json5";
            JsonValue buildProfile = new JsonValue(FileUtil.readFileContent(buildProfilePath));
            buildProfile.arrayList("modules").add(model);
            FileUtil.write(buildProfilePath, buildProfile.toJsonString(), true);
            LogUtil.i("NewNavRouterModel copyResourceFolderToProject", "build-profile.json5中NavRouter模块引用添加成功");

            // 添加三方库引用
            String ohPackagePath = this.basePath + "/oh-package.json5";
            JsonValue ohPackage = new JsonValue(FileUtil.readFileContent(ohPackagePath));

            if (!ohPackage.existence("dependencies")) {
                ohPackage.addMap("dependencies", new HashMap<>());
            }

            ohPackage.value("dependencies").addString("navrouter", "file:./common/NavRouter");
            FileUtil.write(ohPackagePath, ohPackage.toJsonString(), true);
            LogUtil.i("NewNavRouterModel copyResourceFolderToProject", "oh-package.json5中NavRouter模块引用添加成功");
        } catch (IOException ex) {
            LogUtil.e("NewNavRouterModel copyResourceFolderToProject", "Error " + ex.getMessage());
        }
    }

    private void customInit(String navPageNamesPath, InitNavrouterModelEnum model) {
        try {
            // 将配置文件移动到根目录
            FileUtil.copyResourceFolderToProject("packages/routerInfo", this.basePath);
            LogUtil.i("NewNavRouterModel copyResourceFolderToProject", "NavRouter模块复制成功");


            // 创建@tangs/navrouter引用
            String ohPackagePath = this.basePath + "/oh-package.json5";
            JsonValue ohPackage = new JsonValue(FileUtil.readFileContent(ohPackagePath));

            if (!ohPackage.existence("dependencies")) {
                ohPackage.addMap("dependencies", new HashMap<>());
            }

            ohPackage.value("dependencies").addString("@tangs/navrouter", "^1.0.4");
            FileUtil.write(ohPackagePath, ohPackage.toJsonString(), true);
            FileUtil.refreshAndFindFileByPath(ohPackagePath);
            LogUtil.i("NewNavRouterModel copyResourceFolderToProject", "@tangs/navrouter引用添加成功");


            // 将 NavPageNames.ts 文件创建并保存到所选文件夹
            String navNamePath = this.basePath + "/" + navPageNamesPath + "/NavPageNames.ets";
            String pageNamesContent = "export class NavPageNames {\n  static Home = \"sdfsdf\";\n}";
            FileUtil.write(navNamePath, pageNamesContent, true);
            FileUtil.refreshAndFindFileByPath(navNamePath);
            LogUtil.i("NewNavRouterModel copyResourceFolderToProject", "NavPageNames.ts文件创建成功");


            // 修改 navrouter-info.json 文件的内容
            String navrouterInfoPath = this.basePath + "/navrouter-info.json";
            JsonValue navrouterInfo = new JsonValue(FileUtil.readFileContent(navrouterInfoPath));
            navrouterInfo.value("navNamesPath").setValue(navPageNamesPath + "/NavPageNames.ets");
            String modelStr;
            if (model == InitNavrouterModelEnum.DEFAULT) {
                modelStr = "DEFAULT";
            } else {
                modelStr = "CUSTOM";
            }
            navrouterInfo.value("model").setValue(modelStr);

            String jsonString = navrouterInfo.toJsonString();
            FileUtil.write(navrouterInfoPath, jsonString, true);
            FileUtil.refreshAndFindFileByPath(navrouterInfoPath);
            LogUtil.i("NewNavRouterModel copyResourceFolderToProject", "navrouter-info.json文件修改成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
